import {ActionSheetController, Events, IonicPage, NavController, NavParams} from "ionic-angular";
import {ChangeDetectorRef, Component} from "@angular/core";
import {AddStaffProgressSearch,AddStaffItem} from "../../../model/pages/add-staff/addStaffDetail/addStaffDetail.model";
import {AddStaffDetailProvider} from "../../../providers/add-staff/add-staff-detail/add-staff-detail.provider";
import {BaseInfo} from "../../../model/pages/client/client-detail/client-detail-main.model";
import { MessageBushService } from "../../../services/common/message-bush.service";
import {LoadingService} from "../../../services/common/loading.service";

@IonicPage()
@Component({
    selector: 'page-add-staff-detail',
    templateUrl: 'add-staff-detail.html',
    providers: [AddStaffDetailProvider,LoadingService]
})
export class AddStaffDetailPage {
    public staffData;
    private process:number;
    private isLoaded:boolean = false;
    // 客户跟进数据
    private progressList:any[]=[];
    private addStaffItem:AddStaffItem =new AddStaffItem();
    // 客户跟进请求对象
     private getProgress:AddStaffProgressSearch = new AddStaffProgressSearch();
    // 进入页面状态
    private intoPage:boolean;
    private isIonViewDidLoad:boolean=false;
    progressRequest:boolean;
    constructor(private navCtrl: NavController,
                public navParams: NavParams,
                private actionSheetCtrl: ActionSheetController,
                private events: Events,
                private changeRef: ChangeDetectorRef,
                private addStaffDetailProvider: AddStaffDetailProvider,
                private load :LoadingService
    ) {
        this.staffData=this.navParams.get('recruit');
    }
    ionViewDidLoad(){
        this.isIonViewDidLoad = true;
    }
    ionViewDidEnter(){
        this.getProgress.clientId=this.staffData.recruitId;
        this.getProgress.clientType=3;
        this.getProgress.limit=3;
        this.intoPage=true;
        let flag = MessageBushService.getMessage(MessageBushService.keys.addStuffSaveSuccess);
        if(flag == "1" || this.isIonViewDidLoad){
            this.load.showLoading();
            this.getStaffDetail();
            this.isIonViewDidLoad = false;
        }
        this.getProgressList();
    }
    getStaffDetail(){
        this.isLoaded = false;
        this.addStaffDetailProvider.getStaffDetail(this.staffData.recruitId).subscribe((res)=>{
           this.addStaffItem = res;
           this.addStaffItem.process = this.staffData.process;
           this.isLoaded = true;
           this.load.dismissLoading();
           if(this.addStaffItem.onbroadingTime&&this.addStaffItem.onbroadingTime!=''){
               this.process = 4;
           }else if(this.addStaffItem.itcTime&&this.addStaffItem.itcTime!=''){
               this.process = 3;
           }else if(this.addStaffItem.virtualTime&&this.addStaffItem.virtualTime!=''){
               this.process = 2;
           }else if(this.addStaffItem.candidateTime&&this.addStaffItem.candidateTime!=''){
               this.process = 1;
           }else{
               this.process = 0;
           }
        },err=>{
            this.isLoaded = true;
            this.load.dismissLoading();
        });
    }
    // 获取跟进
    getProgressList(){
         this.addStaffDetailProvider.getProgressList(this.getProgress).subscribe((res)=>{
            this.progressList=res['notes'];
            this.progressRequest=true;
         },err=>{
             this.progressRequest=true;
         })
    }
    //跳转到客户笔记
    goToNote(){
        let base: BaseInfo = new BaseInfo();
        base.clientId = this.staffData.recruitId;
        base.clientName = this.addStaffItem.name;
        this.addStaffItem.mobileList&&this.addStaffItem.mobileList.length>0&&this.addStaffItem.mobileList[0]&&this.addStaffItem.mobileList[0].mobile?base.clientPhone = this.addStaffItem.mobileList[0].mobile:null;
        base.clientType = 3;
        this.intoPage=false;
        this.navCtrl.push('ClientNoteListPage', {
            clientNum: this.staffData.recruitId,
            base: base
        })
    }
    //跳转到编辑界面
    editStaff() {
        this.intoPage=false;
        this.navCtrl.push('AddNewStaffPage',{'data':this.addStaffItem})
    }
    //获取子女数量
    getChildNum(num):string{
        if(!num)
            return "";
        if(num<4&&num>0){
            return (num-1)+'个';
        }else{
            return "3个以上";
        }
    }
    /**
     *  返回
     */
    backToProPage() {
        this.intoPage=false;
        this.navCtrl.pop(null);
    }
}
